<%@ page import="dao.Dao" %>
<%@ page import="domain.Goods" %>
<%@ page import="domain.User" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="domain.Order" %><%--
  Created by IntelliJ IDEA.
  User: wangh
  Date: 2020/12/26
  Time: 13:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>订单信息</title>

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery-3.3.1.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <script src="js/bootstrap.min.js"></script>
    <script>
        function checkForm() {
            let checkSnumber1 = checkSnumber();
            let checkAddress1 = checkAddress();
            let checkReceiveName1 = checkReceiveName();
            let checkTele1 = checkTele();
            console.log(checkSnumber1);
            console.log(checkAddress1);
            console.log(checkReceiveName1);
            console.log(checkTele1);
            return checkSnumber1 && checkAddress1 && checkReceiveName1 && checkTele1;
        }

        function checkSnumber() {
            let snumber = document.getElementById('snumber');
            let snumberErr = document.getElementById('snumberErr');
            if (snumber.value.length == 0) {
                snumberErr.innerHTML = "商品数量不能为空"
                snumberErr.className = "error"
                return false;
            } else {
                snumberErr.innerHTML = "OK"
                snumberErr.className = "success";
                return true;
            }
        }

        function checkAddress() {
            let address = document.getElementById('address');
            let addressErr = document.getElementById('addressErr');
            if (address.value.length == 0) {
                addressErr.innerHTML = "收货地址不能为空"
                addressErr.className = "error"
                return false;
            } else {
                addressErr.innerHTML = "OK"
                addressErr.className = "success";
                return true;
            }
        }

        function checkReceiveName() {
            let receivename = document.getElementById('receivename');
            let receivenameErr = document.getElementById('receivenameErr');
            if (receivename.value.length == 0) {
                receivenameErr.innerHTML = "收货人不能为空"
                receivenameErr.className = "error"
                return false;
            } else {
                receivenameErr.innerHTML = "OK"
                receivenameErr.className = "success";
                return true;
            }
        }

        function checkTele() {
            let tele = document.getElementById('tele');
            let teleErr = document.getElementById('teleErr');
            let pattern = /^1[34578]\d{9}$/; //验证手机号正则表达式
            if (!pattern.test(tele.value)) {
                teleErr.innerHTML = "电话号码不合规范"
                teleErr.className = "error"
                return false;
            } else if (tele.value.length == 0) {
                teleErr.innerHTML = "电话号码不能为空"
                teleErr.className = "error"
                return false;
            } else {
                teleErr.innerHTML = "OK"
                teleErr.className = "success";
                return true;
            }
        }

        function checkPayMode(cprice, cdiscounte) {
            if(checkForm()){
                let elementById = document.getElementById("paymode");
                let elementById1 = document.getElementById("snumber");
                if (elementById.value == "网上付款") {
                    console.log(parseFloat(cprice));
                    console.log(parseInt(cdiscounte));
                    let totalprice = parseFloat(cprice) * parseInt(cdiscounte) / 10 * parseInt(elementById1.value);
                    totalprice = parseFloat(totalprice).toFixed(1);
                    if (confirm("您需付款金额为：" + totalprice + "元,是否支付？")) {
                        document.getElementById("form").submit();
                    }
                } else {
                    document.getElementById("form").submit();
                }
            }else {
                alert("输入的信息不能为空！")
            }
        }
    </script>
</head>
<body style="background-color: #F2F2F2">
<%
    request.setCharacterEncoding("utf-8");
    if (request.getSession().getAttribute("user_login") == null) {
        request.setAttribute("message", "您还未登录，请先登录");
        request.getRequestDispatcher("/userLogin.jsp").forward(request, response);
    } else {
        String sid = request.getParameter("sid");
        String suserid = request.getParameter("suserid");
        Dao dao = new Dao();
        Goods goodByCid = dao.findGoodByCid(sid);
        User userById = dao.findUserById(Integer.valueOf(suserid));
        //生成订单编号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());
        String format = simpleDateFormat.format(date);
        String orderid = format + goodByCid.getCid() + String.valueOf(userById.getId());
        //生成订单日期
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss");
        format = simpleDateFormat.format(date);
        String orderdate = format;
        String snumber = request.getParameter("snumber");
        Order order = new Order();
        if (snumber == null) {
            order.setOrderid(orderid);
            order.setSid(goodByCid.getCid());
            order.setSuserid(userById.getId());
            order.setOrderdate(orderdate);
            request.setAttribute("order", order);
            request.setAttribute("order_goodByCid", goodByCid);
            request.setAttribute("order_userById", userById);
        } else {
            order.setOrderid(orderid);
            order.setSid(goodByCid.getCid());
            order.setSuserid(userById.getId());
            order.setOrderdate(orderdate);
            order.setSnumber(Integer.valueOf(snumber));
            request.setAttribute("order", order);
            request.setAttribute("order_goodByCid", goodByCid);
            request.setAttribute("order_userById", userById);
        }
    }
%>
<div class="logoBar"
     style="height: 75px;background: #1d7ad9;font-size: 35px;font-weight: bolder;padding-left: 100px;padding-top: 12px;color: white;">
    电脑销售平台
</div>
<div class="container" style="width: 400px;background-color: #ffffff;margin-top: 20px;border-radius: 10px;">
    <h3 style="text-align: center;">订单信息</h3>
    <form action="${pageContext.request.contextPath}/addOrderServlet?cid=${order_goodByCid.cid}&id=${order_userById.id}"
          method="post" onSubmit="return checkForm()" id="form">
        <div class="form-group">
            <label for="orderid">订单编号：</label>
            <input type="text" name="orderid" class="form-control" id="orderid"
                   value="${order.orderid}" readonly="readonly"/>
        </div>
        <div class="form-group">
            <label for="cname">商品名称：</label>
            <input type="text" name="cname" class="form-control" id="cname"
                   value="${order_goodByCid.cname}" readonly="readonly"/>
        </div>
        <div class="form-group">
            <label for="snumber">商品数量：</label>
            <c:if test="${order.snumber!=0}">
                <input type="text" name="snumber" class="form-control" id="snumber"
                       value="${order.snumber}" placeholder="请输入购买商品的数量" oninput="checkSnumber()"/>
                <span id="snumberErr"></span>
            </c:if>
            <c:if test="${order.snumber==0}">
                <input type="text" name="snumber" class="form-control" id="snumber"
                       placeholder="请输入购买商品的数量" oninput="checkSnumber()"/>
                <span id="snumberErr"></span>
            </c:if>
        </div>
        <div class="form-group">
            <label for="username">购买人名称：</label>
            <input type="text" name="username" class="form-control" id="username"
                   value="${order_userById.username}" readonly="readonly"/>
        </div>
        <div class="form-group">
            <label for="orderdate">订单日期：</label>
            <input type="text" name="orderdate" class="form-control" id="orderdate"
                   value="${order.orderdate}" readonly="readonly"/>
        </div>
        <div class="form-group">
            <label for="address">地址：</label>
            <input type="text" name="address" class="form-control" id="address"
                   placeholder="请输入收货地址" oninput="checkAddress()"/>
            <span id="addressErr"></span>
        </div>
        <div class="form-group">
            <label for="receivename">收货人姓名：</label>
            <input type="text" name="receivename" class="form-control" id="receivename"
                   placeholder="请输入收货人姓名" oninput="checkReceiveName()"/>
            <span id="receivenameErr"></span>
        </div>
        <div class="form-group">
            <label for="tele">电话号码：</label>
            <input type="text" name="tele" class="form-control" id="tele"
                   placeholder="请输入联系方式" oninput="checkTele()"/>
            <span id="teleErr"></span>
        </div>
        <div class="form-inline">
            <label>付款方式：</label>
            <select name="paymode" id="paymode">
                <option value="货到付款">货到付款</option>
                <option value="网上付款">网上付款</option>
            </select>
        </div>
        <div class="form-group" style="text-align: center;margin-top: 10px;">
            <a href="javascript:checkPayMode(${order_goodByCid.cprice},${order_goodByCid.cdiscounte});"><input
                    class="btn btn btn-primary" type="button" value="提交订单"></a>
        </div>
    </form>
    <div class="alert alert-warning alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                aria-hidden="true">&times;</span></button>
        <strong>提示信息:</strong>${orderInfo}
    </div>
</div>
</body>
</html>